package cn.juforg.clean;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import cn.juforg.constant.Constants;
import cn.juforg.ui.MainFrame;
import cn.juforg.util.EncryptUtil;

/**
 * @author Juforg
 * 
 */
public class CleanXp {
	
	public static Properties properties ;
	private FileInputStream inputFile;
//	private FileOutputStream outputFile;
	private String filepath = "src/conf.properties";
	private static StringBuffer sb = new StringBuffer();
	private static int sleepTime = 200;
	static int count ;
	static List<String> fileEndList = new ArrayList();
	private static MainFrame frame;
	private static int count_fail;
	
	public CleanXp(MainFrame frame) {
		this.frame = frame;
		properties = new Properties();
		try {
			inputFile = new FileInputStream(filepath);
			properties.load(inputFile);
			inputFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.getValue();
		}
	
	public void startClean(){
		deleteFile("src", fileEndList);
		
	}
	
	public void getValue(){
		count = properties.size();
		for (int i = 0; i < count; i++) {
			if (properties.containsKey(""+i)) {
				fileEndList.add(properties.getProperty(""+i));
			}
		}
		if (properties.containsKey("key")) {
			EncryptUtil.getKey(properties.getProperty("key"));
		}
		if (properties.containsKey("imgpath")) {
			Constants.IMG_PATH = properties.getProperty("imgpath");
		}
		if (properties.containsKey("millis")) {
			Constants.MILLIS = Long.valueOf(properties.get("millis").toString()) ;
		}
		if (properties.containsKey("host")) {
			Constants.HOST = properties.getProperty("host");
		}
		if (properties.containsKey("from")) {
			Constants.FROM = properties.getProperty("from");
		}
		if (properties.containsKey("to")) {
			Constants.TO = properties.getProperty("to");
		}
		if (properties.containsKey("password")) {
			//Constants.PASSWORD = properties.getProperty("password");
			Constants.PASSWORD = EncryptUtil.getDesString(properties.getProperty("password"));
		}
	}
	public void deleteFile(String fileDir,List fileEndList){
		File file = new File(fileDir);
		if (!file.exists()) {
			sb.append((new StringBuilder()).append(file.getAbsoluteFile()).append("\u4E0D\u5B58\u5728\uFF01").toString());
			sb.append("\n");
			frame.addText(sb.toString());
			try {
				Thread.sleep(sleepTime);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(sb.toString());
			return;
		}
		File allFiles[] = file.listFiles();
		int j = allFiles.length;
		for (int i = 0; i < j; i++) {
			File tmp  = allFiles[i];
			if (fileEndList != null & fileEndList.size()>0) {
				for (Iterator iterator = fileEndList.iterator(); iterator
						.hasNext();) {
					String str = (String) iterator.next();
					if (tmp.getName().endsWith(str)) {
						deleteFileAndLog(tmp);
					}
				}
			}
		}
	}
	
	private static void deleteFileAndLog(File f){
		try {
		f.delete();
		sb.append((new StringBuilder("\u5220\u9664\u6587\u4EF6\u5939\uFF1A")).append(f.getAbsolutePath()).append(" \u6210\u529F\u3002").toString());
		sb.append("\n");
		Thread.sleep(sleepTime);
		frame.addText(sb.toString());
		} catch (InterruptedException e) {
			e.printStackTrace();
			count_fail++;
			sb.append((new StringBuilder("\u5220\u9664\u6587\u4EF6\u5939\uFF1A")).append(f.getAbsolutePath()).append(" \u5931\u8D25\u3002\u539F\u56E0:").append(e).toString());
			sb.append("\n");
			frame.addText(sb.toString());
		}
		
	}
	
}
